﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SPVisualDev
{
    public partial class DialogTargetURL : Form
    {
        private const int C_MAX_RECENT_URLS = 15;
        private List<string> m_recent_urls = new List<string>();

        public DialogTargetURL()
            : this("Select URL")
        {
        }

        public DialogTargetURL(string DialogTitle)
        {
            InitializeComponent();

            this.FormClosing += new FormClosingEventHandler(DialogTargetURL_FormClosing);
            this.cboTargetURL.KeyDown += new KeyEventHandler(cboTargetURL_KeyDown);
            
            this.Text = DialogTitle;
            lblMessage.Text = DialogTitle;
        }

        public List<string> RecentUrls
        {
            get { return m_recent_urls; }
            set { m_recent_urls = value; }
        }

        void cboTargetURL_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
                {
                    SetTargetURL(cboTargetURL.Text);

                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                DialogError.ShowError(ex);
            }
        }

        void DialogTargetURL_FormClosing(object sender, FormClosingEventArgs e)
        {
            //TargetURL = cboTargetURL.Text;
        }

        public string TargetURL
        {
            get;
            private set;
        }

        public void SetTargetURL(string targetUrl)
        {
            if (string.IsNullOrEmpty(targetUrl))
                return;

            if (!targetUrl.StartsWith("http://", StringComparison.InvariantCultureIgnoreCase))
                return;

            if (m_recent_urls.Contains(targetUrl))
                m_recent_urls.Remove(targetUrl);

            if (m_recent_urls.Count > C_MAX_RECENT_URLS)
                m_recent_urls.RemoveAt(C_MAX_RECENT_URLS);

            m_recent_urls.Insert(0, targetUrl);

            cboTargetURL.Items.Clear();
            foreach (string s in m_recent_urls)
                cboTargetURL.Items.Add(s);

            cboTargetURL.Text = targetUrl;
            TargetURL = targetUrl;
        }

        private void btnPick_Click(object sender, EventArgs e)
        {
            try
            {
                using (DialogSharePointExplorer explorer = new DialogSharePointExplorer())
                {
                    explorer.RootUrl = "http://localhost";
                    explorer.Message = "Choose a site to use as target.";
                    explorer.ShowLists = false;
                    explorer.UseWOW64BitCompatibleMode = Common.CheckIf32bitOn64();

                    DialogResult res = explorer.ShowDialog();
                    if (res != DialogResult.OK)
                        return;

                    if (!string.IsNullOrEmpty(explorer.SelectedUrl))
                        if (explorer.SelectedUrl.ToLower().StartsWith("http://"))
                            SetTargetURL(explorer.SelectedUrl);
                        else
                            MessageBox.Show("No valid location selected.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            catch (Exception ex)
            {
                DialogError.ShowError(ex);
            }

        }

        private void DialogTargetURL_Load(object sender, EventArgs e)
        {

            foreach (string s in m_recent_urls)
                cboTargetURL.Items.Add(s);

            if (cboTargetURL.Items.Count > 0)
                cboTargetURL.SelectedIndex = 0;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                SetTargetURL(cboTargetURL.Text);

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            catch (Exception ex)
            {
                DialogError.ShowError(ex);
            }
        }
    }
}
